package org.itboys.shop.service.Manager;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.apache.commons.lang3.StringUtils;
import org.itboys.commons.CommonConstants.DF;
import org.itboys.commons.CommonConstants.NUM;
import org.itboys.commons.CommonConstants.StaticParam;
import org.itboys.commons.utils.tools.ImagePathUtils;
import org.itboys.member.entity.Member;
import org.itboys.member.service.MemberService;
import org.itboys.mongodb.utils.page.Page;
import org.itboys.product.entity.Product;
import org.itboys.product.entity.ProductBrand;
import org.itboys.product.entity.ProductSpecDetail;
import org.itboys.product.entity.ProductText;
import org.itboys.product.service.GoodsSourceService;
import org.itboys.product.service.ProductBrandService;
import org.itboys.product.service.ProductCategorySpecDetailService;
import org.itboys.product.service.ProductSpecDetailService;
import org.itboys.product.service.ProductTextService;
import org.itboys.shop.dto.pro.WXProDetailDto;
import org.itboys.shop.dto.pro.WXProListDto;
import org.itboys.shop.dto.pro.WXProSpecDto;
import org.itboys.trade.dto.WXCommentListDto;
import org.itboys.trade.entity.Comment;
import org.itboys.trade.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
@Service
public class WXProManager {
//	@Autowired
//	private ProductService productService;
	@Autowired
	private GoodsSourceService sourceService;
//	@Autowired
//	private CityService cityService;
	@Autowired
	private ProductBrandService productBrandService;
	@Autowired
	private ProductTextService productTextService;
//	@Autowired
//	private ShopProductCategoryService shopProductCategoryService;
//	@Autowired
//	private ProductCategorySpecService productCategorySpecService;
//	@Autowired
//	private ProductSpecService specService;
//	@Autowired
//	private ProductCategorySpecService catSpecService;
	@Autowired
	private ProductCategorySpecDetailService catSpecDetailService;
	@Autowired
	private ProductSpecDetailService specDetailService;
	@Autowired
	private CommentService commentService;
	@Autowired
	private MemberService memberService;
	public WXProListDto getWXProListDto( Product product){
		return getWXProListDto(product, null);
	}
	private WXProListDto getWXProListDto( Product product,WXProListDto dto) {
		if (null==dto) {
			dto=new WXProListDto();
		}
		dto.setName(product.getName());
		dto.setImage(ImagePathUtils.add(StaticParam.imagePath, product.getImage()));
		dto.setPrice(NUM.DF_00.format(product.getPrice()/100d));
		dto.setProPrice(NUM.DF_00.format(product.getProPrice()/100d));
		dto.setId(product.getId());
		dto.setInfo(product.getScheduleDesc());
		dto.setLocation(sourceService.getSourceCityName(product.getSource()));
		return dto;
	}
	public WXProDetailDto getWXProDetailDto( Product product){
		WXProDetailDto dto=(WXProDetailDto) getWXProListDto(product, new WXProDetailDto());
		dto.setDeliverPrice(NUM.DF.format(0d));//product.get);
		ProductText text=productTextService.getByField("productId", product.getId());
		dto.setDesc(null==text?"":text.getProductText());
		dto.setImages(null==product.getImages()?Lists.newArrayList():product.getImages().stream().map(p->{
			return ImagePathUtils.add(StaticParam.imagePath, p);
		}).collect(Collectors.toList()));
		dto.setSoldCount(String.valueOf(product.getSoldCount()));
		List<Long>cats=product.getCategoryId();
		dto.setCatId(cats.size()>0?cats.get(cats.size()-1):1l);
		List<ProductSpecDetail>specs=specDetailService.findByField("productId", product.getId());
		dto.setSpecs(specs.size()<1?Lists.newArrayList():specs.stream().map(spec->{
			return new WXProSpecDto(
					spec.getId(),
					getSpecName(spec.getCategorySpecDetailIds()), 
					spec.getStock().toString(), 
					NUM.DF_00.format(Double.valueOf(spec.getPrice())));
			
		}).collect(Collectors.toList()));
		ProductBrand brand=productBrandService.getById(product.getBrandId());
		dto.setBrandName(null==brand?"":brand.getName());
		dto.setWeight(String.valueOf(product.getWeigth()));
		Page<WXCommentListDto>comments=commentService.getWXCommentListDtos(product.getId(),null);
		dto.setCommentCount(comments.getTotal());
		dto.setComments(comments.getData());
		return dto;
	}
	public String getSpecName(List<Long>detailIds){
		StringBuilder str=new StringBuilder("");
		catSpecDetailService.getByIds(detailIds).forEach(s->{
			str.append(s.getName()).append("/");
		});
		return StringUtils.isBlank(str)?"":str.substring(0, str.length()-1);
	}
	public String getSpecName(Long specId){
		ProductSpecDetail spec=specDetailService.getById(specId);
		return getSpecName(spec);
	}
	public String getSpecName(ProductSpecDetail spec){
		return null==spec?"":getSpecName(spec.getCategorySpecDetailIds()).replace("/", " ");
	}
}
